{% extends 'wxchat/wxbase.html' %}{% load static %}

{% block tabpanel %}
        <div class="weui-tab__content" style="display: block">
            <div class="weui-panel weui-panel_access">
                <div class="weui-panel__hd">
                    <div class="weui-flex">
                        <div class="weui-flex__item"><span class="title">新手课堂</span></div>
                        <div class="weui-flex__item"></div>
                        <div class="weui-flex__item"><a href="{% url 'dog-index' %}"><i class="icon iconfont icon-shouye2 pink r" ></i></a></div>
                    </div>
                </div>
                <div class="weui-panel__bd" id="freshman"></div>
                <div class="weui-panel__ft" >
                    <a href="javascript:void(0);" class="weui-cell weui-cell_access weui-cell_link" id="freshmanmore" style="display: none">
                        <div class="weui-cell__bd">查看更多</div>
                        <span class="weui-cell__ft"></span>
                    </a>
                </div>
            </div>
        </div>

{% endblock tabpanel %}
{% block tabbar %}
    {% include 'wxchat/include/bottom_nav.html' %}
{% endblock tabbar %}
{% block bottomjs %}
    {{ block.super }}
    <script type="application/javascript">
     nextUrl = '{% url 'dog-freshman-list' %}';
{#     var loading = weui.loading('加载中...');#}
     ///缓存滚动条位置
     $('.weui-tab__panel').scroll(function(){
            if($(this).scrollTop() !=0 ){
                sessionStorage.setItem('offsetTop',$(this).scrollTop());
            }
    });
    $(function(){

        $('#freshmanmore').on('click',function(){
            getDataList( nextUrl );
        });
        getDataList( nextUrl );
    });
    function getDataList(url){
        $.ajax({
          type: 'GET',
          url: url,
          dataType: 'json',
          timeout: 5000,
          context: $('#freshman'),
          success: function(data){
              nextUrl = data.next;
              appendItem(data.results);
              if(nextUrl == null)
                $('#freshmanmore').css('display','none');
              else
                 $('#dfreshmanmore').css('display','');
              setScollPos();
          },
          error: function(xhr, type,error){
              console.log(type);
          }
        });
    }

    function appendItem(results){
        $.each(results,function(index,item){
           var img_url = item.thumb_url;
            if(img_url == null){
                img_url = '<i class="icon iconfont icon-yiliaojigou1  weui-media-box__thumb" ></i>';
            }
            else {
                img_url = '<img class="weui-media-box__thumb" src="' + img_url + '">'
            }
          lossitem =  '<a href="/wechat/freshmandetail/' + item.id + '" class="weui-media-box weui-media-box_appmsg">' +
                            '<div class="weui-media-box__hd">' + img_url + '</div>'+
                            '<div class="weui-media-box__bd">' +
                               '<h4 class="weui-media-box__title">'+ item.title +'</h4>' +
                               '<p class="weui-media-box__desc">'+ item.desc +'</p>'  +
                             '</div>' +
                        '</a>';
            $('#freshman').append( lossitem );
        }) ;
    }
    ////设置滚动条位置
    function setScollPos(){
        var _offset = sessionStorage.getItem("offsetTop");//获取滚动位置
        $('.weui-tab__panel').scrollTop(_offset);
    }

    </script>
{% endblock bottomjs %}
